package com.laker.autotest.job;


import com.laker.autotest.utils.TestNGUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.PostConstruct;
import java.util.Date;

@Component
@Slf4j
@ConditionalOnProperty(name = "easy.cronjob.enabled", havingValue = "true", matchIfMissing = true)
public class CronJob {
    private String testNgXmlPath;

    @PostConstruct
    public void init() {
        testNgXmlPath = TestNGUtil.getTestNgXmlPath();
        log.info("CronJob init,testNgXmlPath is {}.", testNgXmlPath);
    }

    @Scheduled(fixedDelayString = "${easy.cronjob.fixedDelayMillis}")
    public void testNGJob() {
        log.info("Starting TestNG job");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        TestNGUtil.runWithTestNgXmlPath(testNgXmlPath);
        stopWatch.stop();
        log.info("TestNG job completed ,cost {}ms", stopWatch.getTotalTimeMillis());
    }


    // 晚上6点到23点每3小时一次
    @Scheduled(cron = "0 0 18-23/3 * * *")
    public void executeTask1() {
        log.info("执行任务1：{}", new Date());
    }

    // 凌晨6点到晚上8点每小时一次
    @Scheduled(cron = "0 0 6-20 * * *")
    public void executeTask2() {
        log.info("执行任务2：{}", new Date());
    }
}
